#!/bin/bash

set -e

# if there is no user.email, we'll assume it's a new machine/setup and ask it
if [ -z "$(git config --global --get user.email)" ]; then
    read -p ">> What is your github author name? " -r user_name
    read -p ">> What is your github author email? " -r user_email

    git config --global user.name "$user_name"
    git config --global user.email "$user_email"
elif [ "$(git config --global --get dotfiles.managed)" != "true" ]; then
    # if user.email exists, let's check for dotfiles.managed config. If it is
    # not true, we'll backup the gitconfig file and set previous user.email and
    # user.name in the new one
    user_name="$(git config --global --get user.name)"
    user_email="$(git config --global --get user.email)"

    # mv ~/.gitconfig ~/.gitconfig.backup

    # echo "Moved ~/.gitconfig to ~/.gitconfig.backup"

    git config --global user.name "$user_name"
    git config --global user.email "$user_email"
# else
    # otherwise this gitconfig was already made by the dotfiles
    # echo "Already managed by Dotfiles"
fi

# include the gitconfig.local file
# git config --global include.path ~/.gitconfig.local
# finally make git knows this is a managed config already, preventing later
# overrides by this script
git config --global dotfiles.managed true
